const Koa = require('koa');
const app = new Koa();
const router = require("koa-router")();
const bodyparser = require('koa-bodyparser');
const { validator_MpNum } = require('uregexp');
const { Login, Registry, Add ,Updata,Del,Select} = require("./server/mysql")


//登录接口
router.post("/login", async (ctx, next) => {
    const { name, pwd } = ctx.request.body;
    // validator_MpNum = arg.search(/^1\d{10}$/) > -1 ? true : false;
    const xiaoyan = validator_MpNum(name + "");     // => true
    const res = await Login(`select * from user where name='${name}' and password='${pwd}' `)
    if (xiaoyan) {
        if (res.length > 0) {
            ctx.body = {
                code: 200,
                msg: "登录成功",
                res
            }
        } else {
            ctx.body = {
                code: 401,
                msg: "用户名不存在或密码错误"
            }
        }
    } else {
        ctx.body = {
            code: 401,
            msg: "请输入正确的手机号"
        }
    }
})

//注册接口
router.post("/registry", async (ctx, next) => {
    const { name, pwd } = ctx.request.body;
    const res = await Login(`select * from user where name='${name}'`)
    if (res.length > 0) {
        ctx.body = {
            code: 401,
            msg: "用户名已存在"
        }
    } else {
        await Registry(`INSERT INTO user(name,password) VALUES(${name},${pwd})`)
        ctx.body = {
            code: 200,
            msg: "注册成功"
        }
    }
})

//9、正确实现添加分类接口
router.post("/add", async (ctx, next) => {
    const { classify } = ctx.request.body;
    const res = await Add(`INSERT INTO list(classify) VALUES('${classify}')`)
    ctx.body = {
        code: 200,
        msg: "添加成功",
    }
})

//10、正确实现编辑分类接口
router.post("/updata", async (ctx, next) => {
    const { id, classify } = ctx.request.body;
    await Updata(`UPDATE list SET classify = '${classify}' where id=${id}`)
    ctx.body = {
        code: 200,
        msg: "修改成功",
    }
})

//11、正确实现删除分类接口
router.delete("/delete", async (ctx, next) => {
    const { id ,classify } = ctx.query;
    await Del(`DELETE FROM list WHERE classify = '${classify}' and id=${id}`)
    ctx.body = {
        code: 200,
        msg: "删除成功",
    }
})
//12、正确实现获取分类接口
router.get("/select", async (ctx, next) => {
    const {id, classify } = ctx.query;
    const res = await Select(`select * from list where classify='${classify}' and id='${id}' `)
    if(res.length>0){
        ctx.body = {
            code: 200,
            msg: "获取分类成功",
            res
        }
    }else{
        ctx.body = {
            code: 401,
            msg: "获取分类失败",
        }
    }
})


router.get("/list", async (ctx, next) => {
    const res = await Select(`select * from list`)
    if(res.length>0){
        ctx.body = {
            code: 200,
            msg: "获取分类成功",
            res
        }
    }else{
        ctx.body = {
            code: 401,
            msg: "获取分类失败",
        }
    }
})


app.use(bodyparser());
app.use(router.routes(), router.allowedMethods())
app.listen(3030);